﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WizardBase;

namespace OrangePrj
{
    public partial class CreateNewProjectWizard : Form
    {
        private User user;
        private FileReader fileReader;
        private Project project;

        public Project Project
        {
            get
            {
                return project;
            }
        }
        public CreateNewProjectWizard() : this (new User(), new FileReader(new FormMain()))
        {
            
        }

        public CreateNewProjectWizard(User user, FileReader fileReader)
        {
            InitializeComponent();
            this.user = user;
            this.fileReader = fileReader;

            #if DEBUG
            project = new Project("Test", "Test Description");
            DisplayProjectInfo();
            #endif
   
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OpenProjectButton_Click(object sender, EventArgs e)
        {
            if (fileReader.openItem(sender, e) == 0)
            {
                this.Close();
            }
        }
        private void CreateProjectButton_Click(object sender, EventArgs e)
        {
            FormNewPrj newProjectDialog = new FormNewPrj();
            newProjectDialog.ShowDialog();
            if(newProjectDialog.DialogResult == DialogResult.OK)
            {
                this.project = new Project(newProjectDialog.ProjectName, newProjectDialog.Description);
                DisplayProjectInfo();
                DisplayAllWorksInList();
            }
        }
        private void wizardControl1_FinishButtonClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void AddWorkButton_Click(object sender, EventArgs e)
        {
            errorWorkLabel.Visible = false;
            FormAddWork addWorkDialog = new FormAddWork();
            addWorkDialog.ShowDialog();
            if (addWorkDialog.DialogResult == DialogResult.OK)
            {
                project.AddWork(addWorkDialog.work);
                DisplayAllWorksInList();
            }
        }

        /// <summary>
        /// Обрабатывает нажатие клавиши Next. 
        /// </summary>
        private void wizardControl1_NextButtonClick(object sender, EventArgs e)
        {
            if (wizardControl1.CurrentStepIndex == 0 && project == null)
            {
                wizardControl1.NextButtonEnabled = false;
            }
            errorWorkLabel.Visible = false;
        }
        /// <summary>
        /// Обрабатывает нажатие клавиши Back.
        /// </summary>
        private void wizardControl1_BackButtonClick(object sender, EventArgs e)
        {
            wizardControl1.NextButtonEnabled = true;
            errorWorkLabel.Visible = false;
        }
        /// <summary>
        /// Отображает всю информацию о проекте в соответсующих полях Мастера
        /// </summary>
        /// <returns>Состояние отображения</returns>
        private bool DisplayProjectInfo()
        {
            if (project == null)
            {
                return false;
            }

            wizardControl1.NextButtonEnabled = true;
            this.projectTextLabel.Text = project.Name;
            this.projectLabel.Visible = true;
            this.projectTextLabel.Visible = true;
            if (project.Descr.Length != 0)
            {
                this.DescriptionTextLabel.Text = project.Descr;
                this.descriptionLabel.Visible = true;
                this.DescriptionTextLabel.Visible = true;
                DescriptionTextLabel.Height = DescriptionTextLabel.GetPreferredSize(new Size(DescriptionTextLabel.Width, 0)).Height;
            }
            else
            {
                this.descriptionLabel.Visible = false;
                this.DescriptionTextLabel.Visible = false;
            }
            return true;
        }
        /// <summary>
        /// Отображает список работ в ListView
        /// </summary>
        private void DisplayAllWorksInList()
        {
            workListView.Items.Clear();
            foreach (Work w in project.Worklist)
            {
                ListViewItem item = new ListViewItem(w.Name);
                item.SubItems.Add(w.Descr);
                workListView.Items.Add(item);
            }
            if (workListView.Items.Count == 0)
            {
                EditWorkButton.Enabled = false;
                DeleteWorkButton.Enabled = false;
            } else
            {
                EditWorkButton.Enabled = true;
                DeleteWorkButton.Enabled = true;
            }
        }
        /// <summary>
        /// Удаляет выбранную работу
        /// </summary>
        private void DeleteWorkButton_Click(object sender, EventArgs e)
        {
            if (workListView.SelectedItems.Count == 0)
            {
                errorWorkLabel.Visible = true;
                return;
            }

            errorWorkLabel.Visible = false;

            project.DeleteWork(workListView.SelectedItems[0].Index);
            DisplayAllWorksInList();
        }
        /// <summary>
        /// Позволяет редактировать выбранную работу.
        /// </summary>
        private void EditWorkButton_Click(object sender, EventArgs e)
        {
            if (workListView.SelectedItems.Count == 0)
            {
                errorWorkLabel.Visible = true;
                return;
            }
            errorWorkLabel.Visible = false;

            int index = workListView.SelectedItems[0].Index;
            Work work = project.getWork(index);
            FormAddWork editWorkForm = new FormAddWork(work);
            editWorkForm.ShowDialog();
            if (editWorkForm.DialogResult == DialogResult.OK)
            {
                work = new Work(editWorkForm.name, editWorkForm.descr, editWorkForm.time, editWorkForm.type, editWorkForm.res);
                project.ReplaceWork(work, index);
            }

            DisplayAllWorksInList();

            
        }

    }
}
